﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//使用协程减少Update函数的使用，提高运行性能
public class CoroutineTest2 : MonoBehaviour
{

    public Transform target;
    public float smoothing;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(MyCoroutine(target));
        Debug.Log(Time.time + "dddddddddd" + Time.deltaTime);
    }

    IEnumerator MyCoroutine(Transform target) { 
        while (Vector3.Distance(transform.position, target.position) > 0.5f)
        {
            Debug.Log(Time.time + "....." + Time.deltaTime);
            //transform.position = Vector3.Lerp(transform.position, target.position, smoothing * Time.deltaTime);
            transform.position = Vector3.MoveTowards(transform.position, target.position, smoothing * Time.deltaTime);
            yield return null;  //一下部分下一帧执行，减少了Update（）方法的使用
        }
        print("Reached  the target ");
        yield return new WaitForSeconds(3);
        print("MyCoroutine is now finished ");
    }

    private void OnDisable()
    {
        StopCoroutine(MyCoroutine(target));
    }

}
